/**
 * create by gby 20180925
 */
$(function () {
    obj.init();
});

var obj = new function () {
    'use strict';
    return {
        dataGrid: null,
        list: null,
        params: {
            id: null
        },
        init: function () {
            var $this = this;
            $this.form = new xhForm($('#xh_form'));
            $this.form.parse();
            $this.form.bindRequired($('#bt_modal_confirm',parent.document));
            $this.params.id = addressUri.getParam('id');
            if($this.params.id!=null){
                $.ajax({
                    url: baseUrl+'Track/getClassifyInfo',
                    data:{id:this.params.id},
                    type:'POST',
                    dataType:'json',
                    success:function (data) {
                        $this.form.fill(data);
                        $('#iParentId').combotree('setValue', data['iParentId']);
                        $this.form.bindRequired($('#bt_modal_confirm',parent.document));
                    },
                    error:function (data) {
                        console.log(data);
                    }
                });
                $("#iParentId").combotree({
                    onBeforeSelect : function(node) {
                        var nowNode = $('#iParentId').combotree("tree").tree('find', $this.params.id);
                        if(node['id'] == $this.params.id){
                            return false;
                        }
                        var flag=false;
                        $.each($("#iParentId").combotree("tree").tree('getChildren', nowNode.target),function (index,obj) {
                            if(obj['id']==node['id']){
                                flag=true;
                                return false;
                            }
                        });
                        if(flag){
                            return false;
                        }
                    }
                });
            }else {
                var iParentId=addressUri.getParam('iParentId');
                if(iParentId!=null){
                    $('#iParentId').combotree('setValue', iParentId);
                }
                this.form.bindRequired($('#bt_modal_confirm',parent.document));
            }
            this.listener();
        },
        listener: function () {
            var $this = this;
            $('#bt_modal_confirm',parent.document).click(function () {
                $this.funObj().saveData();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                saveData: function () {
                    $this.form.submit('Track/saveClassify',{},function (res) {
                        if(xhExtra.checkError(res)){
                            $this.funObj().initeVal(res.error);
                            return false;
                        }
                        window.top.xhTop.showSuccess(res.msg);
                        window.top.xhTop.getActiveFrame().obj.dataGrid.treegrid('reload');
                        window.top.xhTop.closeModal();
                    },{check:false});
                },

                //验证报错返回
                initeVal: function (message) {
                    window.top.xhTop.showMessage({type: 'error', title: message});
                    xhExtra.setBtnNormal($('#bt_modal_confirm' , window.top.document));
                }
            }
        }
    }
};